package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.AsyncTaskBizType;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.DeviceTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Huqin on 2020/6/4
 */
@RestController
@RequestMapping("/v1/testDevice")
@Api(value = "设备测试模块")
@Slf4j
@AuthRequired(role = RoleType.ADMIN)
public class DeviceTestController {

    @Autowired
    private DeviceTestService deviceTestService;

    @PostMapping("/reliability")
    @ApiOperation("可靠性测试")
    public void reliabilityTest(@RequestParam String imei) {
        deviceTestService.startTest(AsyncTaskBizType.DEVICE_RELIABILITY, imei);
    }

    @GetMapping("/reliability/count")
    @ApiOperation("可靠性测试已循环次数")
    public BaseResult<?> reliabilityLoopCount(@RequestParam String imei) {
        Integer loopCount = deviceTestService.testLoopCount(AsyncTaskBizType.DEVICE_RELIABILITY, imei);
        return BaseResultFactory.produceResult(Code.SUCCESS, loopCount);
    }

    @PutMapping("/reliability/cancel")
    @ApiOperation("取消可靠性测试")
    public void cancelReliabilityTest(@RequestParam String imei) {
        deviceTestService.cancelTest(AsyncTaskBizType.DEVICE_RELIABILITY, imei);
    }

    @PostMapping("/loop")
    @ApiOperation("循环测试")
    public void loopTest(@RequestParam String imei) {
        deviceTestService.startTest(AsyncTaskBizType.DEVICE_INFINITY_LOOP, imei);
    }

    @GetMapping("/loop/count")
    @ApiOperation("循环测试已循环次数")
    public BaseResult<?> loopTestCount(@RequestParam String imei) {
        Integer loopCount = deviceTestService.testLoopCount(AsyncTaskBizType.DEVICE_INFINITY_LOOP, imei);
        return BaseResultFactory.produceResult(Code.SUCCESS, loopCount);
    }

    @PutMapping("/loop/cancel")
    @ApiOperation("循环测试")
    public void cancelLoopTest(@RequestParam String imei) {
        deviceTestService.cancelTest(AsyncTaskBizType.DEVICE_INFINITY_LOOP, imei);
    }

}
